package yiminsu.com.cn.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yiminsu.com.cn.beans.HomestayImage;
import yiminsu.com.cn.beans.Hostuser;
import yiminsu.com.cn.beans.HotelBedTypeVO;
import yiminsu.com.cn.beans.HotelstoryDictionaries;
import yiminsu.com.cn.beans.HotelType;
import yiminsu.com.cn.beans.HotelTypeAssort;
import yiminsu.com.cn.beans.HotelTypeAssortVO;
import yiminsu.com.cn.beans.HotelTypeBedType;
import yiminsu.com.cn.beans.HotelTypeMoney;
import yiminsu.com.cn.beans.HotelTypeMoneyVO;
import yiminsu.com.cn.beans.LeisurehotelUser;
import yiminsu.com.cn.beans.LeisurehotelUserMerchant;
import yiminsu.com.cn.mapper.HostUserMapper;
import yiminsu.com.cn.service.HostUserService;
import yiminsu.com.cn.utils.FileUtils;
import yiminsu.com.cn.utils.ResultsUtil;
import yiminsu.com.cn.utils.UUIDUtils;

@Service
public class HostUserServiceImpl implements HostUserService{
	
	@Autowired
	private HostUserMapper hostUserMapper;
	
	private ResultsUtil   resultsUtil;
	
	//测试
	public Hostuser Gethostuser(String userid) {
		
		return hostUserMapper.Gethostuser(userid);
	}

	//新增子账号信息
	@Override
	public int insertUser(LeisurehotelUser hotelUser) {
		
		int insertUser = hostUserMapper.insertUser(hotelUser);
		return insertUser;
	}

	//新增父账号和子账号
	@Override
	public ResultsUtil insertMerchant(String id,String fid, String zid) {
		LeisurehotelUserMerchant leisurehotelUserMerchant = new LeisurehotelUserMerchant();
		leisurehotelUserMerchant.setFid(fid);
		leisurehotelUserMerchant.setId(id);
		leisurehotelUserMerchant.setZid(zid);
		int insertMerchant = hostUserMapper.insertMerchant(leisurehotelUserMerchant);
		if(insertMerchant==0) 
		{
			resultsUtil.setMsg("新增父账号和子账号失败");
			resultsUtil.setType(false);			
		}
		else
		{			
			resultsUtil.setMsg("新增父账号和子账号成功");
			resultsUtil.setType(true);		
		}
		return resultsUtil;
	}

	//修改子账号信息
	@Override
	public ResultsUtil updateUser(String zid, String userName, String phoneNumber) {
		
		int updateUser = hostUserMapper.updateUser(zid, userName, phoneNumber);
		if(updateUser==0) 
		{			
			resultsUtil.setMsg("修改子账号信息失败");
			resultsUtil.setType(false);			
		}
		else
		{			
			resultsUtil.setMsg("修改子账号信息成功");
			resultsUtil.setType(true);		
		}
		return resultsUtil;
	}

	//解除子账号管理
	@Override
	public ResultsUtil deleteMerchant(String fid, String zid) {
		
		int deleteMerchant = hostUserMapper.deleteMerchant(fid, zid);
		if(deleteMerchant== 0) 
		{			
			resultsUtil.setMsg("解除子账号失败");
			resultsUtil.setType(false);		
		}
		else
		{			
			resultsUtil.setMsg("解除子账号成功");
			resultsUtil.setType(true);			
		}
		return resultsUtil;
	}

	//根据字典Id查询字典表的数据
	@Override
	public int selectById(String id) {
		int selectById = hostUserMapper.selectById(id);
		return selectById;
	}

	//新增房间价格表
	@Override
	public ResultsUtil insertMoney(String id,Integer hid, String hdid, BigDecimal hmoney) {
		
		HotelTypeMoney hotelTypeMoney=new HotelTypeMoney();
		hotelTypeMoney.setId(id);
		hotelTypeMoney.setHid(hid);
		hotelTypeMoney.setHdid(hdid);
		hotelTypeMoney.setHmoney(hmoney);
		int insertMoney = hostUserMapper.insertMoney(hotelTypeMoney);
		if(insertMoney==0)
		{
			 resultsUtil.setMsg("新增房间价格失败");
			 resultsUtil.setType(false);							
		}else
		{
			 resultsUtil.setMsg("新增房间价格成功");
			 resultsUtil.setType(true);
		}
		return resultsUtil;
	}

	//新增配套设施
	@Override
	public ResultsUtil insertAssort(String id,Integer hid, String tdid) {
		
		HotelTypeAssort hotelTypeAssort=new HotelTypeAssort();
		hotelTypeAssort.setId(id);
		hotelTypeAssort.setHid(hid);
		hotelTypeAssort.setTdid(tdid);
		int insertAssort = hostUserMapper.insertAssort(hotelTypeAssort);
		if(insertAssort==0)
		{
			 resultsUtil.setMsg("新增配套设施失败");
			 resultsUtil.setType(false);							
		}else
		{
			 resultsUtil.setMsg("新增配套设施成功");
			 resultsUtil.setType(true);
		}
		return resultsUtil;
	}

	//新增房型
	@Override
	public ResultsUtil insertRoomType(String id,Integer hid,String did,String dmsid,Integer bedtypenum) {
		
        HotelTypeBedType hotelTypeBedType=new HotelTypeBedType();
        hotelTypeBedType.setId(id);
        hotelTypeBedType.setHid(hid);
        hotelTypeBedType.setDid(did);
        hotelTypeBedType.setDmsid(dmsid);
        hotelTypeBedType.setBedtypenum(bedtypenum);
		int insertRoomType = hostUserMapper.insertRoomType(hotelTypeBedType);
		if(insertRoomType==0)
		{
			 resultsUtil.setMsg("新增房型失败");
			 resultsUtil.setType(false);	
						
		}else
		{
			 resultsUtil.setMsg("新增房型成功");
			 resultsUtil.setType(true);
		}
		return resultsUtil;
	}

	//新增字典表
	@Override
	public int insertDictionaries(HotelstoryDictionaries hotelStoryDictionaries) {
		
		int insertDictionaries = hostUserMapper.insertDictionaries(hotelStoryDictionaries);
		return insertDictionaries;
	}

	//新增房间
	@Override
	public int insertRoom(Integer hotelTypeId, String hotelTypeName,List<String> listimage,
			String hotelImage, Integer rooms, Integer roomt,Integer roomw, Integer roomc,
			String roomarea,Integer residepersonnummax,String roomdescribe) {
		 HotelType hotelType=new HotelType();
		 hotelType.setHotelTypeId(hotelTypeId);
		 hotelType.setHotelTypeName(hotelTypeName);
		 hotelType.setHotelImage(hotelImage);
		 hotelType.setRooms(rooms);
		 hotelType.setRoomt(roomt);
		 hotelType.setRoomw(roomw);
		 hotelType.setRoomc(roomc);
		 hotelType.setRoomarea(roomarea);
		 hotelType.setResidepersonnummax(residepersonnummax);
		 hotelType.setRoomdescribe(roomdescribe);
		 int insertRoom = hostUserMapper.insertRoom(hotelType);
		 for (int i = 0; i < listimage.size(); i++) {
				HomestayImage Image=new HomestayImage();
				Image.setId(FileUtils.GetUUID());
				Image.setQid(String.valueOf(hotelType.getHotelTypeId()));//主表id
				Image.setFilepath(listimage.get(i));
				Image.setFiletype(1);
				Image.setFiletable("hoteltype");
				Image.setFieldname("hotelImage");
				try {
					 hostUserMapper.addHomestayImage(Image);
				} catch (Exception e) {  					
					 System.out.println(e);
				}
				
			}
		return insertRoom ;
	}

	//查询房间信息
	@Override
	public HotelType selectHotelType(Integer hotelTypeId) {
		
		HotelType selectHotelType = hostUserMapper.selectHotelType(hotelTypeId);
		return selectHotelType;
	}

	//查询房间价格
	@Override
	public List<HotelTypeMoneyVO> selectMoney(Integer hid) {
		
		List<HotelTypeMoneyVO> selectMoney = hostUserMapper.selectMoney(hid);
		return selectMoney;
	}

	//查询房间床型
	@Override
	public List<HotelBedTypeVO> selectHotelBed(Integer hid) {
		
		List<HotelBedTypeVO> selectHotelBed = hostUserMapper.selectHotelBed(hid);
		return selectHotelBed;
	}

	//查询房间配套设施
	@Override
	public List<HotelTypeAssortVO> selectAssort(Integer hid) {
		
		List<HotelTypeAssortVO> selectAssort = hostUserMapper.selectAssort(hid);
		return selectAssort;
	}
	
	//修改房间
	@Override
	public int updateRoom(Integer hotelTypeId, String hotelTypeName,List<String> listimage, String hotelImage,
			Integer rooms, Integer roomt,Integer roomw, Integer roomc, String roomarea, 
			Integer residepersonnummax,String roomdescribe)
	{		
		HotelType hotelType=new HotelType();
		hotelType.setHotelTypeId(hotelTypeId);
		hotelType.setHotelTypeName(hotelTypeName);
		hotelType.setHotelImage(hotelImage);
		hotelType.setRooms(rooms);
		hotelType.setRoomt(roomt);
		hotelType.setRoomw(roomw);
		hotelType.setRoomc(roomc);
		hotelType.setRoomarea(roomarea);
		hotelType.setResidepersonnummax(residepersonnummax);
		hotelType.setRoomdescribe(roomdescribe);
		int updateRoom = hostUserMapper.updateRoom(hotelType);
		for (int i = 0; i < listimage.size(); i++) {
			HomestayImage homestayImage=new HomestayImage();
			homestayImage.setId(FileUtils.GetUUID());
			homestayImage.setQid(String.valueOf(hotelTypeId));//主表id
			homestayImage.setFilepath(listimage.get(i));
			homestayImage.setFiletype(1);
			homestayImage.setFiletable("hoteltype");
			homestayImage.setFieldname("hotelImage");
			try {
				 hostUserMapper.updateHomestayImage(homestayImage);
			} catch (Exception e) {  					
				 System.out.println(e);
			}		
		}		
		return updateRoom;
		
	}

	//修改配套设施中间表
	@Override
	public ResultsUtil updateAssort(Integer hid,String tdid) {
		
		HotelTypeAssort hotelTypeAssort=new HotelTypeAssort();
		hotelTypeAssort.setHid(hid);
		hotelTypeAssort.setTdid(tdid);
		int updateAssort = hostUserMapper.updateAssort(hotelTypeAssort);
		if(updateAssort==0) 
		{
			resultsUtil.setMsg("修改配套设施失败");
			resultsUtil.setType(false);
		}else
		{
			resultsUtil.setMsg("修改配套设施成功");
			resultsUtil.setType(true);			
		}			
		return resultsUtil;
	}

	//修改房间价格
	@Override
	public ResultsUtil updatePrice(Integer hid,String hdid,BigDecimal hmoney) {
		
		HotelTypeMoney hotelTypeMoney=new HotelTypeMoney();
		hotelTypeMoney.setHid(hid);
		hotelTypeMoney.setHdid(hdid);
		hotelTypeMoney.setHmoney(hmoney);
		int updatePrice = hostUserMapper.updatePrice(hotelTypeMoney);
		if(updatePrice==0)
		{
			 resultsUtil.setMsg("修改房间价格失败");
			 resultsUtil.setType(false);	
						
		}else
		{
			 resultsUtil.setMsg("修改房间价格成功");
			 resultsUtil.setType(true);
		}
		return resultsUtil;
	}

	//修改房间床型		
	@Override
	public int updateRoomType(Integer hid,String did,String dmsid,Integer bedtypenum) {
		
		HotelTypeBedType hotelTypeBedType=new HotelTypeBedType();
		hotelTypeBedType.setHid(hid);
		hotelTypeBedType.setDid(did);
		hotelTypeBedType.setDmsid(dmsid);
		hotelTypeBedType.setBedtypenum(bedtypenum);
		int updateRoomType = hostUserMapper.updateRoomType(hotelTypeBedType);
		return updateRoomType;
	}

	//修改字典表
	@Override
	public ResultsUtil updateDictionaries(String id, String dvalue) {
		
		HotelstoryDictionaries hotelstoryDictionaries=new HotelstoryDictionaries();
		hotelstoryDictionaries.setId(id);
		hotelstoryDictionaries.setDvalue(dvalue);
		int updateDictionaries = hostUserMapper.updateDictionaries(hotelstoryDictionaries);
		if(updateDictionaries==0)
		{
			 resultsUtil.setMsg("修改字典表失败");
			 resultsUtil.setType(false);	
						
		}else
		{
			 resultsUtil.setMsg("修改字典表成功");
			 resultsUtil.setType(true);
		}
		return resultsUtil;
	}

	//修改房态
	@Override
	public ResultsUtil updateStatus(Integer hotelTypeId,Integer roomstatus,Date roomtime) {
		
		int updateNumber = hostUserMapper.updateStatus(hotelTypeId,roomstatus,roomtime);
		if(updateNumber==0)
		{
			 resultsUtil.setMsg("修改房态失败");
			 resultsUtil.setType(false);	
						
		}else
		{
			 resultsUtil.setMsg("修改房态成功");
			 resultsUtil.setType(true);
		}
		return resultsUtil;
	}

	//新增字典表
	@Override
	public ResultsUtil insertStroyValue(String id, String dname, String dvalue,Integer dtype) {
		
		HotelstoryDictionaries hotelstoryDictionaries=new HotelstoryDictionaries();
		hotelstoryDictionaries.setId(id);
		//给字典表中的值设为活动价格
		hotelstoryDictionaries.setDname("活动价格");
		hotelstoryDictionaries.setDvalue(dvalue);
		//字典表设置dtype为2 代表活动价格  
		hotelstoryDictionaries.setDtype(2);
		int insertStroyValue = hostUserMapper.insertStroyValue(hotelstoryDictionaries);
		if(insertStroyValue==0)
		{
			 resultsUtil.setMsg("新增字典表失败");
			 resultsUtil.setType(false);							
		}else
		{
			 resultsUtil.setMsg("新增字典表成功");
			 resultsUtil.setType(true);
		}
		return resultsUtil;
	}

	//新增房间价格
	@Override
	public int insertHotelPrice(String id, Integer hid, String hdid, BigDecimal hmoney) {
		
		HotelTypeMoney hotelTypeMoney=new HotelTypeMoney();
		hotelTypeMoney.setId(id);
		hotelTypeMoney.setHid(hid);
		//设置字典表中的Id
		hotelTypeMoney.setHdid("26");
		hotelTypeMoney.setHmoney(hmoney);
		int insertHotelPrice = hostUserMapper.insertHotelPrice(hotelTypeMoney);		
		return insertHotelPrice;
	}

}
